package service

import (
	chessgame "chess/config"
	"chess/config/chat"
	"chess/database"
	log2 "chess/log"
	"chess/pkg"
	"chess/tools"
	"context"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"google.golang.org/grpc"
	"log"
	"math/rand"
	"net/http"
	"strconv"
	"time"
)

// 存储当前游戏应该下什么棋子
var isBlack = make(map[int]bool)

// 存储当前游戏的持黑棋是哪个用户
var userBlack = make(map[int]int)

// 存储当前游戏的持白棋是哪个用户
var userWriter = make(map[int]int)

var GameRoomList = make(map[int]int)

type ChessGame struct {
	chessgame.UnimplementedChessGameServer
}

//var ChatMessage=make(map[string][...]string)

var GameRecord = make(map[int][]string)

// ShowGameList 查询房间列表
func (cg ChessGame) ShowGameList(ctx context.Context, in *chessgame.ShowGameRoomListRequest) (response *chessgame.ShowGameRoomListResponse, err error) {
	response = &chessgame.ShowGameRoomListResponse{}
	response.RoomList = make(map[int32]int32)
	tools.Rwm.Lock()
	for roomID, playerNum := range GameRoomList {
		log.Println(roomID)
		log.Println(playerNum)
		response.RoomList[int32(roomID)] = int32(playerNum)
	}
	tools.Rwm.Unlock()
	writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "CreateChessRoom", strconv.Itoa(int(in.UserId)), "Debug", "Query room list successfully")
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err2 != nil {
		fmt.Println("写入日志失败")
	}
	return response, err
}

func (cg ChessGame) ViewGameBattleRecords(ctx context.Context, in *chessgame.ViewBattleRecordsRequest) (response *chessgame.ViewBattleRecordsResponse, err error) {
	response = &chessgame.ViewBattleRecordsResponse{}
	tools.Rwm.RLock()
	record, ok := GameRecord[int(in.RoomId)]
	tools.Rwm.RUnlock()
	if ok {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "ViewGameBattleRecords", strconv.Itoa(int(in.UserId)), "Debug", "Successful query of combat records")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		response.Status = http.StatusOK
		response.BattleRecords = record
		return response, err
	}

	getGameRecord, err := database.GetGameRecord(ctx, int(in.RoomId))
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "ViewGameBattleRecords", strconv.Itoa(int(in.UserId)), "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return nil, err
	}
	if len(getGameRecord) > 0 {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "ViewGameBattleRecords", strconv.Itoa(int(in.UserId)), "Debug", "Successful query of combat records")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		response.Status = http.StatusOK
		response.BattleRecords = getGameRecord
		return response, err
	}
	writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "ViewGameBattleRecords", strconv.Itoa(int(in.UserId)), "Error", "unknown return")
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err2 != nil {
		fmt.Println("写入日志失败")
	}
	return nil, errors.New("unknown return")
}

// CreateChessRoom 创建房间
func (cg ChessGame) CreateChessRoom(ctx context.Context, in *chessgame.CreateRoomRequest) (response *chessgame.CreateRoomResponse, err error) {
	response = &chessgame.CreateRoomResponse{}
	err = pkg.CreateBoard(int(in.RoomId))
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "CreateChessRoom", strconv.Itoa(int(in.PlayerAId)), "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		response.Status = http.StatusBadRequest
		return response, err
	}
	// 申明当前游戏为pvp还是pve
	isPve := in.IsPve
	err = database.JoinGame(ctx, int(in.PlayerAId), int(in.RoomId), isPve)
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "CreateChessRoom", strconv.Itoa(int(in.PlayerAId)), "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		response.Status = http.StatusBadRequest
		return response, err
	}
	response.Status = http.StatusOK
	tools.Rwm.Lock()
	// 申明创建房间的人持黑子
	isBlack[int(in.RoomId)] = true
	// 将持黑子用户id写入map
	userBlack[int(in.RoomId)] = int(in.PlayerAId)

	response.Value = GetBoard(int(in.RoomId))
	GameRoomList[int(in.RoomId)] = 1
	if isPve {
		userWriter[int(in.RoomId)] = 999
		GameRoomList[int(in.RoomId)] = 2
	}
	tools.Rwm.Unlock()

	return response, err
}

var (
	ChatAddr = flag.String("ChatAddr", "chat:16012", "the address to connect to")
)

var conn, err = grpc.Dial(*ChatAddr, grpc.WithInsecure())
var c = chat.NewProdChatSystemServiceClient(conn)

func (cg ChessGame) JoinChessRoom(ctx context.Context, in *chessgame.JoinRoomRequest) (response *chessgame.JoinRoomResponse, err error) {
	response = &chessgame.JoinRoomResponse{}
	err = database.JoinGame(ctx, int(in.PlayerBId), int(in.RoomId), false)
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "JoinChessRoom", strconv.Itoa(int(in.PlayerBId)), "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		response.Status = http.StatusBadRequest
		return response, err
	}
	response.Status = http.StatusOK
	tools.Rwm.Lock()
	userWriter[int(in.RoomId)] = int(in.PlayerBId)
	response.Value = GetBoard(int(in.RoomId))
	_, ok := GameRoomList[int(in.RoomId)]
	if ok {
		GameRoomList[int(in.RoomId)] = 2
	}
	if ok == false {
		response.Status = http.StatusBadRequest
		tools.Rwm.Unlock()
		return response, errors.New("房间不存在")
	}
	//GameRoomList[int(in.RoomId)] = 2
	tools.Rwm.Unlock()
	/*bureau, err := c.ChatInTheGameBureau(ctx, &chat.ChatInTheGameBureauRequest{
		GameRoomId: in.RoomId,
		PlayerAId:  int32(userBlack[int(in.RoomId)]),
		PlayerBId:  in.PlayerBId,
	})*/
	chatRoomId := strconv.Itoa(userBlack[int(in.RoomId)]) + strconv.Itoa(int(in.PlayerBId))
	roomid, err := strconv.Atoi(chatRoomId)
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "JoinChessRoom", strconv.Itoa(int(in.PlayerBId)), "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
	}
	form, err := c.GetUserCreateChatRoomForm(ctx, &chat.CreateChatRoomRequest{
		RoomId:    int32(roomid),
		UserId:    int32(userBlack[int(in.RoomId)]),
		IsPrivate: true,
	})
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "JoinChessRoom", strconv.Itoa(int(in.PlayerBId)), "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		response.Status = http.StatusBadRequest
		return response, err
	}
	if form.Status != 200 {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "JoinChessRoom", strconv.Itoa(int(in.PlayerBId)), "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		response.Status = http.StatusBadRequest
		return response, err
	}
	userForm, err := c.GetUserChatRoomMasterAddUserForm(ctx, &chat.ChatRoomMasterAddUserRequest{
		RoomId:   int32(roomid),
		UserId:   in.PlayerBId,
		MasterId: int32(userBlack[int(in.RoomId)]),
	})
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "JoinChessRoom", strconv.Itoa(int(in.PlayerBId)), "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		response.Status = http.StatusBadRequest
		return response, err
	}
	if userForm.Status != 200 {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "JoinChessRoom", strconv.Itoa(int(in.PlayerBId)), "Warring", strconv.Itoa(int(userForm.Status)))
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		response.Status = http.StatusBadRequest
		return response, err
	}
	/*log.Println(bureau.ChatRoomId)
	log.Println(int(bureau.ChatRoomId))*/

	response.Status = http.StatusOK
	response.ChatRoomId2 = strconv.Itoa(roomid)
	return response, err
}

type gameRecord struct {
	Time          string
	ChessPosition string
	ColorPiece    string
	Result        string
}

func (cg ChessGame) PlayChessGame(ctx context.Context, in *chessgame.PlayGameRequest) (response *chessgame.PlayGameResponse, err error) {
	response = &chessgame.PlayGameResponse{}
	value := chessgame.PlayGameResponse{}.Value
	response.Value = value
	// 判断是否为pve游戏
	isPveGame, err := database.IsPveGame(ctx, int(in.RoomId), int(in.PlayerId))
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChessGame", "roomid:"+strconv.Itoa(int(in.RoomId))+",userid:"+strconv.Itoa(int(in.PlayerId)), "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		response.Value = nil
		return response, err
	}
	// 获取当前下棋者是否为黑棋
	log.Println(in.RoomId)
	tools.Rwm.RLock()
	b, ok := isBlack[int(in.RoomId)]
	tools.Rwm.RUnlock()
	//log.Println(b)
	if ok == false {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChessGame", "roomid:"+strconv.Itoa(int(in.RoomId))+",userid:"+strconv.Itoa(int(in.PlayerId)), "Warring", "The room does not exist")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		//log.Println(1)
		return nil, errors.New("room does not exist")
	}

	if b == true {
		tools.Rwm.RLock()
		vb, ok := userBlack[int(in.RoomId)]
		tools.Rwm.RUnlock()
		if ok == false {
			writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChessGame", "roomid:"+strconv.Itoa(int(in.RoomId))+",userid:"+strconv.Itoa(int(in.PlayerId)), "Warring", "The room does not exist")
			if writeLog == 200 {
				fmt.Println("成功写入日志")
			}
			if err2 != nil {
				fmt.Println("写入日志失败")
			}
			log.Println(2)

			return nil, errors.New("room does not exist")
		}
		tools.Rwm.RLock()
		vw, ok := userWriter[int(in.RoomId)]
		tools.Rwm.RUnlock()
		if ok == false && isPveGame != true {
			writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChessGame", "roomid:"+strconv.Itoa(int(in.RoomId))+",userid:"+strconv.Itoa(int(in.PlayerId)), "Warring", "The room is not full")
			if writeLog == 200 {
				fmt.Println("成功写入日志")
			}
			if err2 != nil {
				fmt.Println("写入日志失败")
			}
			log.Println(3)
			return nil, errors.New("the room is not full")
		}
		if vb != int(in.PlayerId) && vw == int(in.PlayerId) {
			writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChessGame", "roomid:"+strconv.Itoa(int(in.RoomId))+",userid:"+strconv.Itoa(int(in.PlayerId)), "Debug", "The white user repeats the game")
			if writeLog == 200 {
				fmt.Println("成功写入日志")
			}
			if err2 != nil {
				fmt.Println("写入日志失败")
			}
			return nil, errors.New("white chess user plays chess repeatedly")
		}
	}
	if b == false {
		tools.Rwm.RLock()
		vw, ok := userWriter[int(in.RoomId)]
		tools.Rwm.RUnlock()
		if ok == false && isPveGame != true {
			writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChessGame", "roomid:"+strconv.Itoa(int(in.RoomId))+",userid:"+strconv.Itoa(int(in.PlayerId)), "Warring", "The room does not exist")
			if writeLog == 200 {
				fmt.Println("成功写入日志")
			}
			if err2 != nil {
				fmt.Println("写入日志失败")
			}
			return nil, errors.New("room does not exist")
		}
		tools.Rwm.RLock()
		vb, ok := userBlack[int(in.RoomId)]
		tools.Rwm.RUnlock()
		if ok == false {
			writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChessGame", "roomid:"+strconv.Itoa(int(in.RoomId))+",userid:"+strconv.Itoa(int(in.PlayerId)), "Warring", "The room is not full")
			if writeLog == 200 {
				fmt.Println("成功写入日志")
			}
			if err2 != nil {
				fmt.Println("写入日志失败")
			}
			return nil, errors.New("the room is not full")
		}
		if vw != int(in.PlayerId) && vb == int(in.PlayerId) {
			writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChessGame", "roomid:"+strconv.Itoa(int(in.RoomId))+",userid:"+strconv.Itoa(int(in.PlayerId)), "Debug", "The black user repeats the game")
			if writeLog == 200 {
				fmt.Println("成功写入日志")
			}
			if err2 != nil {
				fmt.Println("写入日志失败")
			}
			return nil, errors.New("black chess user plays chess repeatedly")
		}
	}
	log.Println(in.CoordinatesX, in.CoordinatesY)
	//	向棋盘下棋
	_, err = pkg.PlayChess(int(in.RoomId), int(in.CoordinatesX), int(in.CoordinatesY), b)
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChessGame", "roomid:"+strconv.Itoa(int(in.RoomId))+",userid:"+strconv.Itoa(int(in.PlayerId)), "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		tools.Rwm.RLock()
		board := GetBoard(int(in.RoomId))
		tools.Rwm.RUnlock()
		response.Value = board

		return response, err
	}
	str := "user:" + strconv.Itoa(int(in.PlayerId)) + "\tIn the room" + strconv.Itoa(int(in.RoomId)) + "\tThe coordinates(" + strconv.Itoa(int(in.CoordinatesX)) + "," + strconv.Itoa(int(in.CoordinatesY)) + ")"
	writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChessGame", "roomid:"+strconv.Itoa(int(in.RoomId))+",userid:"+strconv.Itoa(int(in.PlayerId)), "Debug", str)
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err2 != nil {
		fmt.Println("写入日志失败")
	}
	var color string
	if b {
		color = "黑棋"
	} else {
		color = "白棋"
	}
	var combatLog = gameRecord{
		Time:          time.Now().Format("2006-01-_2 3:04:05.000 PM Mon Jan"),
		ChessPosition: "(" + strconv.Itoa(int(in.CoordinatesX)) + "," + strconv.Itoa(int(in.CoordinatesY)) + ")",
		ColorPiece:    color,
		Result:        "操作成功",
	}
	marshal, err := json.Marshal(combatLog)
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChessGame", "roomid:"+strconv.Itoa(int(in.RoomId))+",userid:"+strconv.Itoa(int(in.PlayerId)), "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return nil, err
	}
	tools.Rwm.Lock()
	GameRecord[int(in.RoomId)] = append(GameRecord[int(in.RoomId)], string(marshal))
	isBlack[int(in.RoomId)] = !b
	tools.Rwm.Unlock()
	check, err := pkg.WinCheck(int(in.RoomId), int(in.CoordinatesX), int(in.CoordinatesY))
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChessGame", "roomid:"+strconv.Itoa(int(in.RoomId))+",userid:"+strconv.Itoa(int(in.PlayerId)), "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		response.Value = GetBoard(int(in.RoomId))
		return response, err
	}
	if check {

		response.Value = GetBoard(int(in.RoomId))
		response.Winner = strconv.Itoa(int(in.PlayerId))
		var loserId int
		if isBlack[int(in.RoomId)] == true {
			loserId = userBlack[int(in.RoomId)]
		}
		if isBlack[int(in.RoomId)] == false {
			loserId = userWriter[int(in.RoomId)]
		}

		err = database.WinGame(ctx, int(in.RoomId), int(in.PlayerId), loserId, in.NickName, GameRecord[int(in.RoomId)])
		if err != nil {
			writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChessGame", "roomid:"+strconv.Itoa(int(in.RoomId))+",userid:"+strconv.Itoa(int(in.PlayerId)), "Warring", err.Error())
			if writeLog == 200 {
				fmt.Println("成功写入日志")
			}
			if err2 != nil {
				fmt.Println("写入日志失败")
			}
			return nil, err
		}
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChessGame", "roomid:"+strconv.Itoa(int(in.RoomId))+",userid:"+strconv.Itoa(int(in.PlayerId)), "Debug", "winer:"+strconv.Itoa(int(in.PlayerId)))
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		delete(GameRoomList, int(in.RoomId))
		delete(userBlack, int(in.RoomId))
		delete(userWriter, int(in.RoomId))
		return response, err
	}
	if isPveGame {
		check, err := PlayWithAl(int(in.RoomId), int(in.CoordinatesX), int(in.CoordinatesY))
		if err != nil {
			writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChessGame", "roomid:"+strconv.Itoa(int(in.RoomId))+",userid:"+strconv.Itoa(int(in.PlayerId)), "Warring", err.Error())
			if writeLog == 200 {
				fmt.Println("成功写入日志")
			}
			if err2 != nil {
				fmt.Println("写入日志失败")
			}
			response.Value = GetBoard(int(in.RoomId))
			return response, err
		}
		if check {
			response.Winner = "AlphaGo"
			response.Value = GetBoard(int(in.RoomId))
			/*atom, err := strconv.Atoi("AlphaGo")
			if err != nil {
				writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChessGame", "roomid:"+strconv.Itoa(int(in.RoomId))+",userid:"+strconv.Itoa(int(in.PlayerId)), "Warring", err.Error())
				if writeLog == 200 {
					fmt.Println("成功写入日志")
				}
				if err2 != nil {
					fmt.Println("写入日志失败")
				}
				atom = 999
			}*/
			err = database.WinGame(ctx, int(in.RoomId), 999, int(in.PlayerId), in.NickName, GameRecord[int(in.RoomId)])
			if err != nil {
				writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChessGame", "roomid:"+strconv.Itoa(int(in.RoomId))+",userid:"+strconv.Itoa(int(in.PlayerId)), "Warring", err.Error())
				if writeLog == 200 {
					fmt.Println("成功写入日志")
				}
				if err2 != nil {
					fmt.Println("写入日志失败")
				}
				return nil, err
			}
			writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChessGame", "roomid:"+strconv.Itoa(int(in.RoomId))+",userid:"+strconv.Itoa(int(in.PlayerId)), "Warring", "winner:AlphaGo")
			if writeLog == 200 {
				fmt.Println("成功写入日志")
			}
			if err2 != nil {
				fmt.Println("写入日志失败")
			}
			delete(GameRoomList, int(in.RoomId))
			delete(userBlack, int(in.RoomId))
			delete(userWriter, int(in.RoomId))
			return response, err
		}
	}
	response.Value = GetBoard(int(in.RoomId))
	return response, err
}
func PlayWithAl(roomId int, x int, y int) (check bool, err error) {
	oldValue, _ := pkg.BoardMap[roomId]
	var value1 [10][10]string
	flag2 := 0

	warring, err, s := pkg.WinCheckWarring(roomId, x, y)
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayWithAl", strconv.Itoa(roomId), "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return false, err
	}
	if warring >= 3 && s == "straight" && flag2 == 0 {
		for i := x; i < pkg.SIZE; i++ {
			if oldValue[i][y] == "+" {
				x = i
				value1, _ = pkg.PlayChess(roomId, i, y, isBlack[roomId])
				flag2 = 1
				break
			}
		}
		for i := x; i > 0; i-- {
			if oldValue[i][y] == "+" {
				x = i
				value1, _ = pkg.PlayChess(roomId, i, y, isBlack[roomId])
				flag2 = 1
				break
			}
		}
	}
	if warring >= 3 && s == "horizontal" && flag2 == 0 {
		for i := y; i < pkg.SIZE; i++ {
			if oldValue[x][i] == "+" {
				y = i
				value1, _ = pkg.PlayChess(roomId, x, i, isBlack[roomId])
				flag2 = 1
				break
			}
		}
		for i := y; i > 0; i-- {
			if oldValue[x][i] == "+" {
				y = i
				value1, _ = pkg.PlayChess(roomId, x, i, isBlack[roomId])
				flag2 = 1
				break
			}
		}
	}
	if warring >= 3 && s == "positiveSlope" && flag2 == 0 {
		for i, j := x, y; i > 0 && j > 0; {
			if oldValue[i][j] == "+" {
				x = i
				y = j
				value1, _ = pkg.PlayChess(roomId, i, j, isBlack[roomId])
				flag2 = 1
				break
			}
			i--
			j--
		}
		for i, j := x, y; i < pkg.SIZE && j < pkg.SIZE; {
			if oldValue[i][j] == "+" {
				x = i
				y = j
				value1, _ = pkg.PlayChess(roomId, i, j, isBlack[roomId])
				flag2 = 1
				break
			}
			i++
			j++
		}
	}
	if warring >= 3 && s == "anticlinal" && flag2 == 0 {
		for i, j := x, y; i > 0 && j < pkg.SIZE; {
			if oldValue[i][j] == "+" {
				x = i
				y = j
				value1, _ = pkg.PlayChess(roomId, i, j, isBlack[roomId])
				flag2 = 1
				break
			}
			i--
			j++
		}
		for i, j := x, y; i < pkg.SIZE && j > 0; {
			if oldValue[i][j] == "+" {
				x = i
				y = j
				value1, _ = pkg.PlayChess(roomId, i, j, isBlack[roomId])
				flag2 = 1
				break
			}
			i++
			j--
		}
	}

	if x <= 8 && oldValue[x+1][y] == "+" && flag2 == 0 {
		value1, _ = pkg.PlayChess(roomId, x+1, y, isBlack[roomId])
		x = x + 1

		flag2 = 1
	}
	if x >= 1 && oldValue[x-1][y] == "+" && flag2 == 0 {
		value1, _ = pkg.PlayChess(roomId, x-1, y, isBlack[roomId])
		x = x - 1
		flag2 = 1
	}
	if y <= 8 && oldValue[x][y+1] == "+" && flag2 == 0 {
		value1, _ = pkg.PlayChess(roomId, x, y+1, isBlack[roomId])
		y = y + 1
		flag2 = 1
	}
	if y >= 1 && oldValue[x][y-1] == "+" && flag2 == 0 {
		value1, _ = pkg.PlayChess(roomId, x, y-1, isBlack[roomId])
		y = y - 1
		flag2 = 1
	}
	if y >= 1 && x >= 1 && oldValue[x-1][y-1] == "+" && flag2 == 0 {
		value1, _ = pkg.PlayChess(roomId, x-1, y-1, isBlack[roomId])
		x = x - 1
		y = y - 1
		flag2 = 1
	}
	if y <= 8 && x >= 1 && oldValue[x-1][y+1] == "+" && flag2 == 0 {
		value1, _ = pkg.PlayChess(roomId, x-1, y+1, isBlack[roomId])
		x = x - 1
		y = y + 1
		flag2 = 1
	}
	if y <= 8 && x <= 8 && oldValue[x+1][y+1] == "+" && flag2 == 0 {
		value1, _ = pkg.PlayChess(roomId, x+1, y+1, isBlack[roomId])
		x = x + 1
		y = y + 1
		flag2 = 1
	}
	if x <= 8 && y >= 1 && oldValue[x+1][y-1] == "+" && flag2 == 0 {
		value1, _ = pkg.PlayChess(roomId, x+1, y-1, isBlack[roomId])
		x = x + 1
		y = y - 1
		flag2 = 1
	}
	if flag2 == 0 {
		//将时间戳设置成种子数
		rand.Seed(time.Now().UnixNano())
		x := rand.Intn(10)
		y := rand.Intn(10)
		log.Println(x, y)
		value1, _ = pkg.PlayChess(roomId, x, y, isBlack[roomId])
	}

	if value1 == oldValue {
		for {
			//将时间戳设置成种子数
			rand.Seed(time.Now().UnixNano())
			x = rand.Intn(10)
			y = rand.Intn(10)
			log.Println(x, y)
			value2, _ := pkg.PlayChess(roomId, x, y, isBlack[roomId])
			fmt.Println(GetBoard(roomId))
			if value2 != oldValue {

				str := "user:" + "AlphaGo" + "In the room" + strconv.Itoa(roomId) + "The coordinates(" + strconv.Itoa(x) + "," + strconv.Itoa(y) + ")"
				writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayWithAl", "roomid"+strconv.Itoa(roomId), "Debug", str)
				if writeLog == 200 {
					fmt.Println("成功写入日志")
				}
				if err2 != nil {
					fmt.Println("写入日志失败")
				}

				log.Println("ok")
				break
			}
		}
	}

	var color string
	if isBlack[roomId] {
		color = "黑棋"
	} else {
		color = "白棋"
	}
	var combatLog = gameRecord{
		Time:          time.Now().Format("2006-01-_2 3:04:05.000 PM Mon Jan"),
		ChessPosition: "(" + strconv.Itoa(x) + "," + strconv.Itoa(y) + ")",
		ColorPiece:    color,
		Result:        "操作成功",
	}
	marshal, err := json.Marshal(combatLog)

	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayWithAl", strconv.Itoa(roomId), "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return false, err
	}
	tools.Rwm.Lock()
	GameRecord[roomId] = append(GameRecord[roomId], string(marshal))

	isBlack[roomId] = !isBlack[roomId]
	tools.Rwm.Unlock()
	winCheck, err := pkg.WinCheck(roomId, x, y)
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayWithAl", strconv.Itoa(roomId), "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return false, err
	}
	return winCheck, err

}
func GetBoard(roomId int) []string {
	tmpBoard := pkg.BoardMap[roomId]
	board := make([]string, 0)
	var strFirst string
	strFirst = " "
	for i := 0; i < pkg.SIZE; i++ {
		strFirst += strconv.Itoa(i)
	}
	board = append(board, strFirst)
	for i := 0; i < pkg.SIZE; i++ {
		var str string
		str = strconv.Itoa(i)
		for j := 0; j < pkg.SIZE; j++ {
			str += tmpBoard[i][j]
		}

		board = append(board, str)
	}
	for i, s := range board {
		fmt.Println(i, s)
	}
	return board
}
